local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__zhuji",
  tags = { LB_U.LeaderSkill },
}

Fk:loadTranslationTable {
  ["lb_zero__zhuji"] = "筑羁",
  [":lb_zero__zhuji"] = "<b><a href='lb_leader_skill'><font color='red'>领袖技</font></a></b>，" ..
      "“白祇重工”的角色使用【杀】无视防具，且使用时同势力角色可以弃置其区域内一张与上张助战牌类型不同的牌为之“助战→响应牌数+1”。",

  ["#lb_zero__zhuji-zhuzhan1"] = "助战：你可以弃置一张牌，助战 %src 使用的 %arg 响应牌数+1",
  ["#lb_zero__zhuji-zhuzhan2"] = "助战：你可以弃置一张非%arg，助战 %src 使用的 %arg2 响应牌数+1",

  ["@lb_zero__zhuji"] = "筑羁",

  ["$lb_zero__zhuji1"] = "不要妨碍我！",
  ["$lb_zero__zhuji2"] = "看我把你锤进地基！",
}

skel:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.card.trueName == "slash" and
    LB_U.CompareKingdomWith(target, "lb_zero_baiqizhonggong")
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(), function(p)
      return not p:isAllNude() and LB_U.CompareKingdomWith(p, "lb_zero_baiqizhonggong")
    end)
    if #targets > 0 then
      local pattern = "."
      local prompt = "#lb_zero__zhuji-zhuzhan1:" .. player.id .. "::" .. data.card:toLogString()
      local not_type = player:getMark("@lb_zero__zhuji")
      if not_type ~= 0 then
        pattern = ".|.|.|.|.|^" .. not_type
        prompt = "#lb_zero__zhuji-zhuzhan2:" .. player.id .. "::" .. not_type .. ":" .. data.card:toLogString()
      end
      local req = Request:new(targets, "AskForUseActiveSkill")
      req.focus_text = "@zhuzhan"
      req.n = 1
      for _, p in ipairs(targets) do 
        local cards = table.filter(p:getCardIds("hej"), function (id)
          return Exppattern:Parse(pattern):match(Fk:getCardById(id, false)) and not p:prohibitDiscard(id)
        end)
        local extra_data = {
          num = 1,
          min_num = 1,
          include_equip = true,
          skillName = skel.name,
          pattern = tostring(Exppattern {id = cards}),
          expand_pile = p:getCardIds("j"),
        }
        req:setData(p, {
          "choose_cards_skill",
          prompt,
          true,
          extra_data,
        })
      end
      req:ask()
      local winner = req.winners[1]
      if winner then
        local result = req:getResult(winner)
        local ids = result
        if result ~= "" then
          if result.card then
            ids = result.card.subcards
          else
            ids = result
          end
        end
        event:setCostData(self, { cards = ids, from = winner })
        room:useSkill(player, self, function()
          self:use(event, target, player, data)
        end, event.skill_data)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(event:getCostData(self).cards[1])
    local from = event:getCostData(self).from
    room:throwCard(card, skel.name, from)
    room:setPlayerMark(player, "@lb_zero__zhuji", card:getTypeString())
    data.extra_data = data.extra_data or {}
    data.extra_data.lb_zero__zhuji = player
  end,
})

skel:addEffect(fk.TargetConfirmed, {
  can_refresh = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.lb_zero__zhuji == data.from and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    data:setResponseTimes(data:getResponseTimes() + 1)
  end,
})

skel:addEffect(fk.TargetSpecified, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.card.trueName == "slash" and
        LB_U.CompareKingdomWith(target, "lb_zero_baiqizhonggong")
  end,
  on_use = function(self, event, target, player, data)
    data.to:addQinggangTag(data)
  end,
})

return skel
